﻿using System.Collections.Generic; 
using System.Linq; 
using DevExpress.Utils; 
using YDServices;
using YDModel;
using YDCommon;
using System;
using System.Windows.Forms;
using System.Drawing;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using System.Data;

namespace YDWin.OrderReport
{
    public partial class OrderReportForm : DevExpress.XtraEditors.XtraForm
    {

        List<V_Order_Report> opList = null;
        V_Order_Report item = null; 
        OrderReportService svr = new OrderReportService();

        public OrderReportForm()
        {
            InitializeComponent();

            Global.GirdViewSet(this.gvOrderReport);

         
            //合并行
            this.gvOrderReport.OptionsView.AllowCellMerge = true;
            //显示 footer,用于显示汇总行
            //this.gvOrderReport.OptionsView.ShowFooter = true;


            //翻页事件
            pageControl1.OnPageChanged += new EventHandler(pagerControl1_OnPageChanged);

            //设置日期控件格式
            this.dtpStartDate.Properties.DisplayFormat.FormatString = "yyyy年MM月dd日";
            this.dtpStartDate.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.dtpStartDate.Properties.EditFormat.FormatString = "yyyy年MM月dd日";
            this.dtpStartDate.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.dtpStartDate.Properties.Mask.EditMask = "yyyy年MM月dd日";

            this.dtpEndDate.Properties.DisplayFormat.FormatString = "yyyy年MM月dd日";
            this.dtpEndDate.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.dtpEndDate.Properties.EditFormat.FormatString = "yyyy年MM月dd日";
            this.dtpEndDate.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.dtpEndDate.Properties.Mask.EditMask = "yyyy年MM月dd日";

  
        }
        /// <summary>
        /// 自动行号 自增长
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gvEmbryo_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
            if (e.Info.IsRowIndicator)
            {
                if (e.RowHandle >= 0)
                {
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
                }
                else if (e.RowHandle < 0 && e.RowHandle > -1000)
                {
                    e.Info.Appearance.BackColor = System.Drawing.Color.AntiqueWhite;
                    e.Info.DisplayText = "G" + e.RowHandle.ToString();
                }
            }
        }


        private void btnQuery_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadDataGrid();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadDataGrid()
        {
            WaitDialogForm sdf = new WaitDialogForm("", "正在查询......");

            item = null;
            ConditionItem[] conds = GetConditions();
            if (conds != null)
            {
                opList = svr.getList(conds);
                sdf.Close();
                if (svr.HasError)
                {
                    Utils.ShowPrompt(MessageType.mtWarning, "载入失败\n" + svr.Errors);
                    svr.Errors.Clear();
                }
                else
                {
                    List<V_Order_Report> list = opList.Skip((pageControl1.PageIndex - 1) * pageControl1.PageSize).Take(pageControl1.PageSize).ToList();
                    pageControl1.DrawControl(opList.Count());
                    if (list.Count() == 0)
                    {
                        bsData.DataSource = null;
                    }
                    else
                    {
                        bsData.DataSource = list;
                        this.gvOrderReport.MoveFirst();//移动到第一条记录
                        //UIChecked.BestFit();
                    }
                }
            }
           

        }

        /// <summary>
        /// 查询条件
        /// </summary>
        /// <returns></returns>
        private ConditionItem[] GetConditions()
        {
            List<ConditionItem> conds = new List<ConditionItem>();

            //conds.Add(new ConditionItem
            //{
            //    PropertyName = "Factory_Name",
            //    FindType = ConditionType.ctLike,
            //    Values = new object[] { txtFactoryName.Text }
            //});

            conds.Add(new ConditionItem
            {
                PropertyName = "OperatintTime",
                FindType = ConditionType.ctGreaterThanOrEqual,
                Values = new object[] { dtpStartDate.EditValue }
            });

            conds.Add(new ConditionItem
            {
                PropertyName = "OperatintTime",
                FindType = ConditionType.ctLessThanOrEqual,
                Values = new object[] { dtpEndDate.EditValue }
            });

            return conds.ToArray();
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void pagerControl1_OnPageChanged(object sender, EventArgs e)
        {
            LoadDataGrid();
        }
         
 
  

        /// <summary>
        /// 点击事件与gvEmbryo.OptionsBehavior.EditorShowMode 设置对应
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gvEmbryo_Click(object sender, EventArgs e)
        { 
            item = bsData.Current as V_Order_Report; 
        }
        /// <summary>
        /// 鼠标事件 与gvEmbryo.OptionsBehavior.EditorShowMode 设置对应
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gvEmbryo_MouseDown(object sender, MouseEventArgs e)
        {
            item = bsData.Current as V_Order_Report;
        }


        /// <summary>
        /// 加载完事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EmbryoForm_Load(object sender, EventArgs e)
        {
            dtpStartDate.EditValue = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);//月初1号
            dtpEndDate.EditValue = new DateTime(DateTime.Now.AddDays(1).Year, DateTime.Now.AddDays(1).Month, DateTime.Now.AddDays(1).Day);//当日+1

        }


        /// <summary>
        /// 设置合并条件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gvOrderReport_CellMerge(object sender, CellMergeEventArgs e)
        {
            try
            {

                GridView myGv = sender as GridView;
                if (myGv == null) return;
    
                //胚布 合并设置
                if (e.Column.FieldName.Equals("Order_ID") || e.Column.FieldName.Equals("Factory_Name")
                || e.Column.FieldName.Equals("OperatintTime")
               || e.Column.FieldName.Equals("Quantity") || e.Column.FieldName.Equals("Weight")
                  || e.Column.FieldName.Equals("SumWeight") || e.Column.FieldName.Equals("SerialNumber")
                     || e.Column.FieldName.Equals("Spec_Name") || e.Column.FieldName.Equals("UnitPrice")
                        || e.Column.FieldName.Equals("TotalPrice"))
                { 
                    var dra1 = myGv.GetRow(e.RowHandle2 - 1) as V_Order_Report;
                    var dra2 = myGv.GetRow(e.RowHandle2) as V_Order_Report;
                    if (dra1 == null || dra2 == null) return;

                    if (dra1.Order_ID != dra2.Order_ID)
                    {
                        e.Handled = true;
                    } 
                }

                //磨毛合并设置
                if (e.Column.FieldName.Equals("Sading_ID")
                || e.Column.FieldName.Equals("OperatintTime_Sading") || e.Column.FieldName.Equals("Factory_Name_Sading")
               || e.Column.FieldName.Equals("ProcessUnitPrice_Sading") || e.Column.FieldName.Equals("LossPer_Sading")
                  || e.Column.FieldName.Equals("SingleOrDouble_Sading") || e.Column.FieldName.Equals("SurplusQuantity_Sading")
                     || e.Column.FieldName.Equals("ProcessCost_Sading") || e.Column.FieldName.Equals("Spec_Name_Sading")
                      || e.Column.FieldName.Equals("Quantity_Sading") )
                { 
                    var dra1 = myGv.GetRow(e.RowHandle2 - 1) as V_Order_Report;
                    var dra2 = myGv.GetRow(e.RowHandle2) as V_Order_Report;
                    if (dra1 == null || dra2 == null) return;

                    if (dra1.Sading_ID!= dra2.Sading_ID)
                    { 
                        e.Handled = true;
                    } 
                }


                //印花合并设置
                if (e.Column.FieldName.Equals("Printing_ID")
                || e.Column.FieldName.Equals("OperatintTime_Printing") || e.Column.FieldName.Equals("Factory_Name_Printing")
                || e.Column.FieldName.Equals("Spec_Name_Printing") || e.Column.FieldName.Equals("Quantity_Printing")
                || e.Column.FieldName.Equals("ProcessUnitPrice_Printing") || e.Column.FieldName.Equals("ProcessCost_Printing")
                || e.Column.FieldName.Equals("SurplusQuantity_Printing")   )
                {
                    //if(e.Column.FieldName.Equals("Quantity_Printing"))
                    //{
                    //    int aa = 1;
                    //}
                    var dra1 = myGv.GetRow(e.RowHandle2 - 1) as V_Order_Report;
                    var dra2 = myGv.GetRow(e.RowHandle2) as V_Order_Report;
                    if (dra1 == null || dra2 == null) return;

                    if (dra1.Printing_ID != dra2.Printing_ID)
                    {
                        e.Handled = true;
                    }
                }

                //染厂合并设置
                if (e.Column.FieldName.Equals("Deying_ID")
                 || e.Column.FieldName.Equals("OperatintTime_Deying") || e.Column.FieldName.Equals("Factory_Name_Deying")
                 || e.Column.FieldName.Equals("Spec_Name_Deying") || e.Column.FieldName.Equals("Quantity_Deying")
                 || e.Column.FieldName.Equals("ProcessUnitPrice_Deying") || e.Column.FieldName.Equals("ProcessCost_Deying")
                 || e.Column.FieldName.Equals("SurplusQuantity_Deying"))
                {
                    var dra1 = myGv.GetRow(e.RowHandle2 - 1) as V_Order_Report;
                    var dra2 = myGv.GetRow(e.RowHandle2) as V_Order_Report;
                    if (dra1 == null || dra2 == null) return;

                    if (dra1.Deying_ID != dra2.Deying_ID)
                    {
                        e.Handled = true;
                    }
                }

                //烫金合并设置
                if (e.Column.FieldName.Equals("Handle_ID") || e.Column.FieldName.Equals("HandleType_Handle")
                 || e.Column.FieldName.Equals("OperatintTime_Handle") || e.Column.FieldName.Equals("Factory_Name_Handle")
                 || e.Column.FieldName.Equals("Spec_Name_Handle") || e.Column.FieldName.Equals("Quantity_Handle")
                 || e.Column.FieldName.Equals("ProcessUnitPrice_Handle") || e.Column.FieldName.Equals("ProcessCost_Handle")
                 || e.Column.FieldName.Equals("SurplusQuantity_Handle"))
                {
                    var dra1 = myGv.GetRow(e.RowHandle2 - 1) as V_Order_Report;
                    var dra2 = myGv.GetRow(e.RowHandle2) as V_Order_Report;
                    if (dra1 == null || dra2 == null) return;

                    if (dra1.Handle_ID != dra2.Handle_ID)
                    {
                        e.Handled = true;
                    }
                }

            
                //发货合并设置
                if (e.Column.FieldName.Equals("Sending_ID")
                 || e.Column.FieldName.Equals("OperatintTime_Sending") || e.Column.FieldName.Equals("Customer_Name_Sending")
                 || e.Column.FieldName.Equals("Quantity_Sending") || e.Column.FieldName.Equals("UnitPrice_Sending")
                 || e.Column.FieldName.Equals("TotalPrice_Sending") || e.Column.FieldName.Equals("Cost_Sending")
                 || e.Column.FieldName.Equals("Profit_Sending") || e.Column.FieldName.Equals("Memo_Sending"))
                {
                    var dra1 = myGv.GetRow(e.RowHandle2 - 1) as V_Order_Report;
                    var dra2 = myGv.GetRow(e.RowHandle2) as V_Order_Report;
                    if (dra1 == null || dra2 == null) return;

                    if (dra1.Sending_ID != dra2.Sending_ID)
                    {
                        e.Handled = true;
                    }
                }

            }
            catch
            { }
    
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出Excel";
            fileDialog.Filter = "Excel文件(*.xls)|*.xls";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                gridControl1.ExportToXls(fileDialog.FileName);
                DevExpress.XtraEditors.XtraMessageBox.Show("导出成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            } 
        }
    }
}